package com.un.ebs.businessObject.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.un.ebs.businessObject.service.SQLStrategy;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.un.ebs.core.enums.DataTypeEnum;
import com.un.ebs.core.enums.DbTypeEnum;
import com.un.ebs.core.enums.DefaultFieldEnum;
import com.un.ebs.core.enums.OperatorEnum;
import com.un.ebs.core.metadata.Query;
import com.un.ebs.core.metadata.QueryCondition;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.config.HandlerDbType;
import com.un.ebs.sys.domain.Column;
import com.un.ebs.sys.enums.DisplayEnums;
import com.un.ebs.businessObject.service.SQLSimpleHandler;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author zc
 * @ClassName SQLStrategy
 * @Description MySQL数据库实现
 * @Date 2021/1/14
 */
@Service
@HandlerDbType(DbTypeEnum.mysql)
@Slf4j
public class MySQLStrategy extends SQLSimpleHandler implements SQLStrategy {

	@Override
	public String listSQL(String tableName, String sortName, Map<String, Object> params, List<Column> columnList) {
		// 定义三个sql
		StringBuffer baseSQL = new StringBuffer(" SELECT ");
		StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
		StringBuffer orderBySQL = new StringBuffer(" ORDER BY ");
		String orderByName = "";

		// 是否删除、条件添加
		boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
				.contains(DisplayEnums.IS_DELETE.getColumnName());
		columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
				.collect(Collectors.toList());
		if (is_del) {
			conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
		}

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String columnName = column.getColumnName();
			Object param = null;

			if (!CollectionUtil.isEmpty(params)) {
				param = params.get(columnName);
			}

			if (i != columnList.size() - 1) {
				baseSQL.append(columnName += ",");
			} else {
				baseSQL.append(columnName);
			}

			String paramValue = (param == null ? "" : param.toString());
			if (!StringUtils.isNullOrEmpty(paramValue)) {
				String condition = "";
				DataTypeEnum enumByCode = column.getColumnType();
				// 整数型，布尔型，《等于处理》
				if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {

					condition = " AND " + column.getColumnName() + " = " + param;

				} else if (DataTypeEnum.STRING.equals(enumByCode)) { // 字符型 处理为like

					condition = " AND " + column.getColumnName() + " LIKE '%";
					condition += param + "%'";

				} else if (DataTypeEnum.DATE.equals(enumByCode)) {
					// 格式化日期
					condition = " AND " + column.getColumnName() + "='" + param + "'";
				} else if (DataTypeEnum.OBJECT.equals(enumByCode)) {
					// 对象类型
					condition = " AND " + column.getColumnName() + "='" + param + "'";
				}
				conditionSQL.append(condition);
			}

			if (columnName.equals(sortName)) {
				orderByName = sortName;
				orderBySQL.append(columnName);
			}
		}

		if (StringUtils.isNullOrEmpty(orderByName)) {
			orderBySQL.append("id");
		}
		baseSQL.append(" FROM " + tableName + " ").append(conditionSQL).append(orderBySQL);
		log.debug("listSQL:" + baseSQL.toString());
		return baseSQL.toString();
	}

	@Override
	public String pageListSQL(String tableName, String sortName, Integer pageIndex, Integer pageSize,
			Map<String, Object> params, List<Column> columnList) {
		// 定义三个sql
		StringBuffer baseSQL = new StringBuffer(" SELECT ");
		StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
		StringBuffer orderBySQL = new StringBuffer(" ORDER BY ");
		StringBuffer pageSQL = new StringBuffer();
		String orderByName = "";
		if (pageSize != null && pageIndex != null) {
			Integer num = (pageIndex - 1) * pageSize;
			Integer size = pageSize;
			pageSQL.append(String.format(" limit %d,%d ", num, size));
		}

		// 是否删除、条件添加
		boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
				.contains(DisplayEnums.IS_DELETE.getColumnName());
		columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
				.collect(Collectors.toList());
		if (is_del) {
			conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
		}

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String columnName = column.getColumnName();
			Object param = null;

			if (!CollectionUtil.isEmpty(params)) {
				param = params.get(columnName);
			}

			if (i != columnList.size() - 1) {
				baseSQL.append(columnName += ",");
			} else {
				baseSQL.append(columnName);
			}

			String paramValue = (param == null ? "" : param.toString());
			if (!StringUtils.isNullOrEmpty(paramValue)) {
				String condition = "";
				DataTypeEnum enumByCode = column.getColumnType();
				// 整数型，布尔型，《等于处理》
				if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {

					condition = " AND " + column.getColumnName() + " = " + param;

				} else if (DataTypeEnum.STRING.equals(enumByCode)) { // 字符型 处理为like

					condition = " AND " + column.getColumnName() + " LIKE '%";
					condition += param + "%'";

				} else if (DataTypeEnum.DATE.equals(enumByCode)) {
					// 格式化日期
					condition = " AND " + column.getColumnName() + "='" + param + "'";
				} else if (DataTypeEnum.OBJECT.equals(enumByCode)) {
					// 对象类型
					condition = " AND " + column.getColumnName() + "='" + param + "'";
				} else if (DataTypeEnum.OBJECT.equals(enumByCode)) {
					// 对象类型
					condition = " AND " + column.getColumnName() + "='" + param + "'";
				}
				conditionSQL.append(condition);
			}

			if (columnName.equals(sortName)) {
				orderByName = sortName;
				orderBySQL.append(columnName);
			}
		}

		if (StringUtils.isNullOrEmpty(orderByName)) {
			orderBySQL.append("id");
		}
		baseSQL.append(" FROM " + tableName + " ").append(conditionSQL).append(orderBySQL).append(pageSQL);
		log.debug("pageListSQL:" + baseSQL.toString());
		return baseSQL.toString();
	}

	@Override
	public String pageListCount(String tableName, String sortName, Map<String, Object> params,
			List<Column> columnList) {
		// 定义三个sql
		StringBuffer baseSQL = new StringBuffer(" SELECT COUNT(1) ");
		StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
		StringBuffer orderBySQL = new StringBuffer(" ORDER BY ");
		String orderByName = "";

		// 是否删除、条件添加
		boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
				.contains(DisplayEnums.IS_DELETE.getColumnName());
		columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
				.collect(Collectors.toList());
		if (is_del) {
			conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
		}

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String columnName = column.getColumnName();
			Object param = null;

			if (!CollectionUtil.isEmpty(params)) {
				param = params.get(columnName);
			}
			String paramValue = (param == null ? "" : param.toString());
			if (!StringUtils.isNullOrEmpty(paramValue)) {
				String condition = "";
				DataTypeEnum enumByCode = column.getColumnType();
				// 整数型，布尔型，《等于处理》
				if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {

					condition = " AND " + column.getColumnName() + " = " + param;

				} else if (DataTypeEnum.STRING.equals(enumByCode)) { // 字符型 处理为like

					condition = " AND " + column.getColumnName() + " LIKE '%";
					condition += param + "%'";

				} else if (DataTypeEnum.DATE.equals(enumByCode)) {
					// 格式化日期
					condition = " AND " + column.getColumnName() + "='" + param + "'";
				} else if (DataTypeEnum.OBJECT.equals(enumByCode)) {
					// 对象类型
					condition = " AND " + column.getColumnName() + "='" + param + "'";
				}
				conditionSQL.append(condition);
			}

			if (columnName.equals(sortName)) {
				orderByName = sortName;
				orderBySQL.append(columnName);
			}
		}
		if (StringUtils.isNullOrEmpty(orderByName)) {
			orderBySQL.append("id");
		}

		baseSQL.append(" FROM " + tableName + " ").append(conditionSQL).append(orderBySQL);

		log.debug("pageListCount:" + baseSQL.toString());
		return baseSQL.toString();
	}

	@Override
	public String getById(String tableName, String id, List<Column> columnList) {
		StringBuffer baseSQL = new StringBuffer(" SELECT ");
		StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 AND id=" + "'" + id + "'");

		Optional<Column> colTenantId = columnList.stream()
				.filter(x -> x.getColumnName().equals(DefaultFieldEnum.tenant_id.getValue())).findFirst();
		if (colTenantId.get() != null) {
			conditionSQL
					.append(" AND " + DefaultFieldEnum.tenant_id.getValue() + " = '" + colTenantId.get().getId() + "'");
		}

		// 是否删除、条件添加
		boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
				.contains(DisplayEnums.IS_DELETE.getColumnName());
		columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
				.collect(Collectors.toList());
		if (is_del) {
			conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
		}

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String columnName = column.getColumnName();
			if (i != columnList.size() - 1) {
				baseSQL.append(columnName + ",");
			} else {
				baseSQL.append(columnName);
			}
		}

		baseSQL.append(" FROM " + tableName + " ").append(conditionSQL);
		return baseSQL.toString();
	}

	@Override
	public String add(String tableName, JSONObject json, List<Column> columnList) {
		List<String> collect = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList());

		if (collect.contains(DisplayEnums.IS_DELETE.getColumnName())) {
			json.put(DisplayEnums.IS_DELETE.getColumnName(), "0");
		}

		StringBuffer insertSQL = new StringBuffer(" insert into " + tableName + " ( ");

		StringBuffer keys = new StringBuffer();
		StringBuffer values = new StringBuffer();

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String key = column.getColumnName();
			if (column.getIsPk()) {
				json.put(key, json.getString("id"));
			}
			String value = json.getString(column.getColumnName());

			DataTypeEnum enumByCode = column.getColumnType();

			if (value != null) {
				if (i != columnList.size() - 1) {
					if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
						values.append(value + ",");
					} else {
						values.append("'" + value + "'" + ",");
					}

					keys.append(key + ",");
				} else {
					if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
						values.append(value);
					} else {
						values.append("'" + value + "'");
					}
					keys.append(key);
				}
			}
		}
		insertSQL.append(keys);
		insertSQL.append(" ) ");
		insertSQL.append(" values ( ");
		insertSQL.append(values + " ) ");
		return insertSQL.toString();
	}

	@Override
	public String update(String tableName, JSONObject json, List<Column> columnList) {
		StringBuffer updateSQL = new StringBuffer(" update " + tableName);
		StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 AND id=" + "'" + json.get("id") + "'");
		StringBuffer centerSQL = new StringBuffer();

		Optional<Column> colTenantId = columnList.stream()
				.filter(x -> x.getColumnName().equals(DefaultFieldEnum.tenant_id.getValue())).findFirst();
		if (colTenantId.get() != null) {
			conditionSQL
					.append(" AND " + DefaultFieldEnum.tenant_id.getValue() + " = '" + colTenantId.get().getId() + "'");
		}

		boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
				.contains(DisplayEnums.IS_DELETE.getColumnName());
		columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
				.collect(Collectors.toList());
		if (is_del) {
			conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
		}

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String key = column.getColumnName();
			String value = json.getString(column.getColumnName());
			DataTypeEnum enumByCode = column.getColumnType();

			if (value != null) {
				if (i == 0 || StringUtils.isNullOrEmpty(centerSQL.toString())) {
					if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
						centerSQL.append(" set " + key + "=" + value);
					} else {
						centerSQL.append(" set " + key + "=" + "'" + value + "'");
					}

				} else if (i > 0) {
					if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
						centerSQL.append("," + key + "=" + value);
					} else {
						centerSQL.append("," + key + "=" + "'" + value + "'");
					}
				}
			}
		}

		updateSQL.append(centerSQL).append(conditionSQL);
		return updateSQL.toString();
	}

	@Override
	public String delete(String tableName, String id, Boolean supportLogicDelete, String tenantId) {
		StringBuffer sql = new StringBuffer();
		if (supportLogicDelete) {
			sql.append("update " + tableName + " set is_delete = 1 WHERE 1=1 AND id=" + "'" + id + "'");
		} else {
			sql.append("delete from " + tableName + " WHERE 1=1 AND id=" + "'" + id + "'");
		}

		if (tenantId != null) {
			sql.append(" AND " + DefaultFieldEnum.tenant_id.getValue() + " = '" + tenantId + "'");
		}

		return sql.toString();
	}

	@Override
	public String querySQL(String tableName, Query query, List<Column> columnList) {
		StringBuffer baseSQL = new StringBuffer(" SELECT ");
		StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
		StringBuffer orderBySQL = new StringBuffer(" ORDER BY ");
		StringBuffer pageSQL = new StringBuffer();
		String orderByName = "";

		if (query.getPageSize() != null && query.getPageIndex() != null) {
			Integer num = (query.getPageIndex() - 1) * query.getPageSize();
			Integer size = query.getPageSize();
			pageSQL.append(String.format(" limit %d,%d ", num, size));
		}

		List<QueryCondition> conditions = query.getConditions();

		Map<String, QueryCondition> conditionMap = conditions.stream()
				.collect(Collectors.toMap(QueryCondition::getKey, Function.identity()));

		// 是否删除、条件添加
		boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
				.contains(DisplayEnums.IS_DELETE.getColumnName());
		columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
				.collect(Collectors.toList());
		if (is_del) {
			conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
		}

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String columnName = column.getColumnName();
			if (i != columnList.size() - 1) {
				baseSQL.append(columnName).append(",");
			} else {
				baseSQL.append(columnName);
			}
			QueryCondition queryCondition = conditionMap.get(column.getColumnName());

			// 如果查询条件为空
			if (queryCondition == null || CollectionUtil.isEmpty(queryCondition.getValues())) {
				continue;
			}
			DataTypeEnum enumByCode = column.getColumnType();
			OperatorEnum operatorEnum = OperatorEnum.getEnumByCode(Integer.valueOf(queryCondition.getOperator()));

			if (OperatorEnum.TYPE_NE.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "<>" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "<>" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_EQ.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "=" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "=" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_GT.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + ">" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + ">" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_GE.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_LT.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "<" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "<" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_LE.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "<=" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_LIKE.equals(operatorEnum)) {
				conditionSQL.append(" AND " + columnName + " LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
			} else if (OperatorEnum.TYPE_NOT_LIKE.equals(operatorEnum)) {
				conditionSQL
						.append(" AND " + columnName + " NOT LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
			} else if (OperatorEnum.TYPE_IN.equals(operatorEnum)) {
				// 构造in条件
				List<String> values = queryCondition.getValues();
				String inStr = "";
				for (int j = 0; j < values.size(); j++) {
					if (j != values.size() - 1) {
						inStr += "'" + values.get(j) + "'" + ",";
					} else {
						inStr += "'" + values.get(j) + "'";
					}
				}
				conditionSQL.append(" AND " + columnName + " IN (" + inStr + ")");
			} else if (OperatorEnum.TYPE_SI.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
					conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(1) + "'");

				} else {
					conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
					conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(1));
				}
			}

			if (columnName.equals(query.getSortName())) {
				orderByName = columnName;
				orderBySQL.append(columnName);
			}
		}

		if (StringUtils.isNullOrEmpty(orderByName)) {
			orderBySQL.append("id");
		}

		baseSQL.append(" FROM " + tableName + " ").append(conditionSQL).append(orderBySQL).append(pageSQL).append(" ;");
		log.info("querySQL：" + baseSQL.toString());
		return baseSQL.toString();
	}

	@Override
	public String queryCount(String tableName, Query query, List<Column> columnList) {
		StringBuffer baseSQL = new StringBuffer(" SELECT COUNT(1) ");
		StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");

		// 是否删除、条件添加
		boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
				.contains(DisplayEnums.IS_DELETE.getColumnName());
		columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
				.collect(Collectors.toList());
		if (is_del) {
			conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
		}

		List<QueryCondition> conditions = query.getConditions();

		Map<String, QueryCondition> conditionMap = conditions.stream()
				.collect(Collectors.toMap(QueryCondition::getKey, Function.identity()));

		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			String columnName = column.getColumnName();
			QueryCondition queryCondition = conditionMap.get(column.getColumnName());

			// 如果查询条件为空
			if (queryCondition == null || CollectionUtil.isEmpty(queryCondition.getValues())) {
				continue;
			}
			DataTypeEnum enumByCode = column.getColumnType();
			OperatorEnum operatorEnum = OperatorEnum.getEnumByCode(Integer.valueOf(queryCondition.getOperator()));

			if (OperatorEnum.TYPE_NE.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "<>" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "<>" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_EQ.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "=" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "=" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_GT.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + ">" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + ">" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_GE.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_LT.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "<" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "<" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_LE.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
						|| DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + "<=" + "'" + queryCondition.getValues().get(0) + "'");
				} else {
					conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(0));
				}
			} else if (OperatorEnum.TYPE_LIKE.equals(operatorEnum)) {
				conditionSQL.append(" AND " + columnName + " LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
			} else if (OperatorEnum.TYPE_NOT_LIKE.equals(operatorEnum)) {
				conditionSQL
						.append(" AND " + columnName + " NOT LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
			} else if (OperatorEnum.TYPE_IN.equals(operatorEnum)) {
				// 构造in条件
				List<String> values = queryCondition.getValues();
				String inStr = "";
				for (int j = 0; j < values.size(); j++) {
					if (j != values.size() - 1) {
						inStr += "'" + values.get(j) + "'" + ",";
					} else {
						inStr += "'" + values.get(j) + "'";
					}
				}
				conditionSQL.append(" AND " + columnName + " IN (" + inStr + ")");
			} else if (OperatorEnum.TYPE_SI.equals(operatorEnum)) {
				if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
					conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
					conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(1) + "'");

				} else {
					conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
					conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(1));
				}
			}
		}
		baseSQL.append(" FROM " + tableName + " ").append(conditionSQL);
		log.info("分页sql：" + baseSQL.toString());
		return baseSQL.toString();
	}
}
